package org.jash.sportnews2109.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.databinding.ObservableArrayMap
import com.alibaba.android.arouter.facade.annotation.Route
import org.jash.common.logging
import org.jash.common.mvvm.BaseActivity
import org.jash.common.token
import org.jash.sportnews2109.R
import org.jash.sportnews2109.databinding.ActivityRegisterBinding
import org.jash.sportnews2109.vm.RegisterViewModel
@Route(path = "/news/register")
class RegisterActivity : BaseActivity<ActivityRegisterBinding, RegisterViewModel>() {
    val user by lazy { ObservableArrayMap<String, String>() }

    override fun initView() {
        binding.user = user
        binding.getCode.setOnClickListener {
            viewModel.getVerifyCode(user["phone"] ?: "")
        }
        binding.check.setOnClickListener {
            viewModel.checkVerifyCode(user)
        }
        binding.register.setOnClickListener {
            viewModel.register(user)
        }
    }

    override fun initData() {
        viewModel.verifyLiveData.observe(this, this::getVerifyCode)
        viewModel.checkLiveData.observe(this) {
            Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
        }
        viewModel.registerLiveData.observe(this, this::success)

    }
    fun getVerifyCode(code:String) {
        Toast.makeText(this, "验证码: $code", Toast.LENGTH_SHORT).show()
        user["code"] = code
    }
    fun success(t:String) {
        Toast.makeText(this, t, Toast.LENGTH_SHORT).show()
        finish()
    }
}